package edu.gmu.projects.fireresponder.fusionengine;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class PresenceDecayRunnable implements Runnable {

	private Set<PresenceAnchorPoint> anchors;
	private boolean stopMe;
	private FusionEngine fusion;
	
	public PresenceDecayRunnable(FusionEngine fusion) {
		anchors = Collections.synchronizedSet(new HashSet<PresenceAnchorPoint>());
		this.fusion = fusion;
		stopMe = false;
	}
	
	public void addPresenseAlert(PresenceAnchorPoint anchor) {
		anchors.add(anchor);
	}

	public void stopMe() {
		stopMe = true;
	}
	
	public void run() {
		while(! stopMe) {
			List<PresenceAnchorPoint> completelyDecayed = new ArrayList<PresenceAnchorPoint>();
			List<PresenceAnchorPoint> copy = new ArrayList<PresenceAnchorPoint>();
			copy.addAll(anchors);
			for(PresenceAnchorPoint anchor : copy) {
				anchor.updatePointProbability();
				fusion.updatePresenceProbabilityForAssets(anchor);
				if(! anchor.isActive()) {
					completelyDecayed.add(anchor);
				}
			}
			
				anchors.removeAll(completelyDecayed);
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}


}
